﻿/***********************************************************

Please use , com.cf.core.Utils.cacheXML to load the XML file.
useleass you totally understand what going on here.

//**********************************************************/
package com.cf.core.Utils{
	public class XMLToObject {
		public static function parse(dp:XML,ignoreNamespace:Boolean=false):Object {
			if (dp) {
				var _obj:Object={};
				dp.ignoreWhitespace=true;
				pNode(dp,_obj,ignoreNamespace);
				return _obj;
			}
			return null;
		}

		private static function pNode(node:*,obj:Object,ignoreNamespace:Boolean):void {
			//
			if (ignoreNamespace) {
				node.setNamespace("");
			}//
			var nodeName:String=node.name().toString();
			var o:Object={};
			var j:Object;
			if (node.attributes().length()>0) {
				for (j in node.attributes()) {
					o[node.attributes()[j].name().toString()]=node.attributes()[j];
				}
				if (node.children().length()<=1&&o["value"]==undefined) {
					o["value"]=node.toString();
				}
			} else {
				if (node.children().length()<=1&&! node.hasComplexContent()) {
					o=node.toString();
				}
			}
			//——————————
			if (obj[nodeName]==undefined) {
				obj[nodeName]=o;
			} else {
				if (obj[nodeName] is Array) {
					obj[nodeName].push(o);
				} else {
					obj[nodeName]=[obj[nodeName],o];
				}

			}
			try {
				toObj(node,obj[nodeName],ignoreNamespace);
			} catch (e:Error) {
			}

		}

		static private function toObj(dp:XML,obj:Object,ignoreNamespace:Boolean):void {
			var i:uint,nl:uint;
			nl=dp.children().length();
			var node:XML;
			for (i=0; i<nl; i++) {
				node=dp.children()[i];
				if (obj is Array) {
					pNode(node,obj[obj.length-1],ignoreNamespace);
				} else {
					pNode(node,obj,ignoreNamespace);
				}
			}

		}
	}
}